<?php

class AuthenticationController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function loginAction()
    {
    	$this->view->title = 'Đăng nhập';
        if(Zend_Auth::getInstance()->hasIdentity()){
            $this->_redirect('index/index');
        }
        
        $request = $this->getRequest();
        $form = new Form_LoginForm();
        if($request->isPost()){
            if($form->isValid($this->_request->getPost())){
                $authAdapter = $this->getAuthAdapter();
        
                $tendangnhap = $form->getValue('tendangnhap');
                $matkhau = md5($form->getValue('matkhau'));
                
             //   $matkhau = md5($data['matkhau']);
                $authAdapter->setIdentity($tendangnhap)
                            ->setCredential($matkhau);
                            
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                
                if($result->isValid()){
                    $identity = $authAdapter->getResultRowObject();
                    
                    $authStorage = $auth->getStorage();
                    $authStorage->write($identity);
                    
                    $this->_redirect('index/index');
                } else {
                    $this->view->errorMessage = "Tên đăng nhập hay mật khẩu của bạn không đúng..";
                }
            }
        }
        
        
        $this->view->loginForm = $form;
    }
    public function registerAction(){
    	$this->view->title = 'Đăng ký';
    	$formThem = new Form_NguoiDungForm();
    	 
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/authentication/register');
    	 
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$this->view->form = $formThem;
    	 
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			 
    			$tennd 						= $data['tennguoidung'];
    			$email 						= $data['email'];
    			$tendn 						= $data['tendangnhap'];
    			$matkhau				= md5($data['matkhau']);
    			$khoa 						= '0';
    			$quyenhan 				= 'users';
    			$nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
    			 
    			$nguoidungObj 	= new Library_Model_NguoiDung();
    			$nguoidungObj->setTennd($tennd);
    			$nguoidungObj->setEmail($email);
    			$nguoidungObj->setTendangnhap($tendn);
    			$nguoidungObj->setMatkhau($matkhau);
    			$nguoidungObj->setKhoa($khoa);
    			$nguoidungObj->setQuyenhan($quyenhan);
    			 
    			$nguoidung  = $nguoidungMapper->save($nguoidungObj);
    			$this->_redirect('/index');
    		}
    	}
    }
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('index/index');
    }

    private function getAuthAdapter() 
    {
        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $authAdapter->setTableName('tbl_nguoidung')
                    ->setIdentityColumn('TenDangNhap')
                    ->setCredentialColumn('MatKhau');
                    
        return $authAdapter;
    }
}